#include<iostream>
#include<cstring>
using namespace std;
const int N = 5e2 + 10;
int f[N][N];
int arr[N];
int n;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> arr[i];
	memset(f, 0x3f, sizeof(f));
	for (int i = 1; i <= n; i++)f[i][i] = 1;
	for (int i = 1; i < n; i++) {
		f[i][i + 1] = 1 + (arr[i] == arr[i + 1] ? 0 : 1);
		//cout << "i i+1   " << f[i][i + 1] << endl;
	}
	for (int len = 3; len <= n; len++) {
		for (int l = 1; l + len - 1 <= n; l++) {
			int r = l + len - 1;
			if (arr[l] == arr[r])f[l][r] = f[l + 1][r - 1];
			for (int k = l; k < r; k++) {
				f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r]);
			}
		}
	}
	cout << f[1][n] << endl;
	return 0;
}